package com.neusoft.neuiotms.dcam.fixed.converter.spareconsume;


import com.neusoft.bizcore.web.dto.AbstractConverter;
import com.neusoft.neuiotms.dcam.fixed.converter.StockConverter;
import com.neusoft.neuiotms.dcam.fixed.dto.spareconsume.SpareConsumeStockDTO;
import com.neusoft.neuiotms.dcam.fixed.model.spareconsume.SpareConsumeStock;
import com.neusoft.neuiotms.dcam.fixed.repository.spareConsume.SpareConsumeStockRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class SpareConsumeStockConver extends AbstractConverter<SpareConsumeStock, SpareConsumeStockDTO> {

    @Autowired
    private SpareConsumeStockRepository spareConsumeStockRepository;

    @Autowired
    private StockConverter stockConverter;

    @Override
    public SpareConsumeStock toModel(SpareConsumeStockDTO dto) {
        SpareConsumeStock model;
        if (dto.isNew()) {
            model = new SpareConsumeStock();
        } else {
            model = this.spareConsumeStockRepository.getOne(dto.getId());
        }
        model.setTaskId(dto.getTaskId());
        model.setWorkNum(dto.getWorkOrderNum());
        if(dto.getStock() != null){
            model.setStock(stockConverter.toModel(dto.getStock()));
        }
        return model;
    }

    @Override
    public SpareConsumeStockDTO toDTO(SpareConsumeStock model, boolean forListView) {
        final SpareConsumeStockDTO dto = new SpareConsumeStockDTO();
        dto.setId(model.getId());
        dto.setTaskId(model.getTaskId());
        dto.setWorkOrderNum(model.getWorkNum());
        if(model.getStock() != null){
            dto.setStock(stockConverter.toDTO(model.getStock()));
            dto.setStockId(model.getStock().getId());
            dto.setSn(model.getStock().getSn());
            dto.setSpecification(model.getStock().getSpecification());
            dto.setSource(model.getStock().getSource());
            if(model.getStock().getSpareType() != null ){
                dto.setSpareTypeName(model.getStock().getSpareType().getName());
            }
            if(model.getStock().getStorehouse() != null){
                dto.setStorehouseName(model.getStock().getStorehouse().getName());
            }
            dto.setUnitPrice(model.getStock().getUnitPrice());
            dto.setStorckNumber(model.getStock().getStorckNumber());
        }
        return dto;
    }
}
